package com.google.code.multitester.myapp;

import org.easymock.EasyMock;

/**
 * This class should be initialized in one step by calling {@see
 * AnnieStub#setPhrase} method.
 * <p>
 * The usage of this class is the following:
 * <ol>
 * <li>Client (in terms of Multitester library test code is a client) creates
 * instance of the class.</li>
 * <li>Client initialize created instance by {@see AnnieStub#setPhrase} method.</li>
 * <li>Client calls {@see IStub#say} to get work done by the
 * {@link com.google.code.multitester.myapp.AnnieStub}</li>
 * </ol>
 * <p>
 * The same is true for {@link com.google.code.multitester.myapp.JimmyStub}. But
 * in that class initialization should be done in two steps.
 * <p>
 * 
 * @author Yuriy Sechko
 */
public class AnnieStub implements IStub
{

	private String phrase;

	// These fields are used for checking whether specific method was called or
	// not.
	public static final EasymockStub mockForCheckingSetPhraseCall = EasyMock
			.createStrictMock(EasymockStub.class);
	public static final EasymockStub mockForCheckingCloseCall = EasyMock
			.createStrictMock(EasymockStub.class);

	/**
	 * Specified string will be used in {@see AnnieStub#say} method.
	 * 
	 * @param phrase
	 *            initialization phrase
	 */
	public void setPhrase(final String phrase)
	{
		// signalize to EasyMock that 'setPhrase()' method was executed
		mockForCheckingSetPhraseCall.call();
		this.phrase = phrase;
	}

	/**
	 * Returns specified string concatenated with initialization phrase (which
	 * is set in {@see AnnieStub#setPhrase})
	 * 
	 * @return initialization phrase + whatToSay
	 */
	public String say(final String whatToSay)
	{
		if (whatToSay == null)
		{
			throw new IllegalArgumentException("whatToSay should not be null.");
		}
		return whatToSay + phrase.toString();
	}

	/**
	 * Dummy method for shutting-down logic.
	 */
	public void close()
	{
		// signalize to EasyMock that 'close()' method was executed
		mockForCheckingCloseCall.call();
	}
}
